<template>
  <div class="Box" v-if="ifLogin">
    <div
      class="chatBox"
      v-if="type !== 'notic'"
      :class="{ goRight: userId === currentId }"
    >
      <div>
        <img :src="avatar" alt="" />
      </div>
      <div class="boxRight">
        <p
          class="realname"
          :style="{ textAlign: userId === currentId ? 'right' : 'left' }"
        >
          {{ realname }}
        </p>
        <div
          class="message1"
          v-if="type !== 'notic'"
          :style="{ backgroundColor: userId === currentId ? '#cfe6fd' : '#fff' }"
        >
          {{ message }}
          <div
            :class="[
              { Lborder: userId !== currentId },
              { Rborder: userId === currentId },
            ]"
          ></div>
        </div>
      </div>
    </div>
    <div class="tip" v-if="type === 'notic'">
      {{ message }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, onMounted, ref, computed } from "vue";
import { useUserStore } from "@/store";
const store = useUserStore();
const props = defineProps(["chatInfo"]);
const currentUsername = ref<string>(store.user.username);
const currentrealname = ref<string>("");
const currentId = ref<string>(store.user.id);
const { userId, message, realname, avatar, type } = props.chatInfo;
onMounted(() => {
  const Dom = document.querySelector(".Box");
  ifLogin && Dom && (Dom.scrollTop = Dom?.scrollHeight);
});
const ifLogin = computed(() => store.user.username);
</script>

<style lang="scss" scoped>
.Box {
  position: relative;
  overflow: hidden;
  margin: 18px 0;
}
.goRight {
  float: right;
  margin-right: 40px;
  img {
    position: absolute;
    right: 0;
    float: right;
  }
}
.boxRight {
  margin: 0 12px;
}
.realname {
  // margin-left: 15px;
  color: #666662;
  font-size: 12px;
}
.chatBox {
  display: flex;
  // margin: 16px;
}
img {
  width: 40px;
  height: 40px;
  border-radius: 40px;
}
.tip {
  display: block;
  font-size: 13px;
  color: #666666;
  text-align: center;
  margin: 5px 15px;
}
.message1 {
  float: right;
  max-width: 350px;
  display: inline-block;
  margin: 5px 15px;
  background-color: #ffffff;
  border-bottom-color: #ffffff;
  /*为了给after伪元素自动继承*/
  color: #333;
  font-size: 14px;
  line-height: 18px;
  padding: 8px 15px 8px 15px;
  box-sizing: border-box;
  border-radius: 6px;
  position: relative;
  word-break: break-all;
}

.Rborder {
  position: absolute;
  top: -5px;
  right: -7px;
  width: 10px;
  height: 10px;
  border-width: 0 0 10px 10px;
  border-style: solid;
  border-bottom-color: #cfe6fd;
  /*自动继承父元素的border-bottom-color*/
  border-left-color: transparent;
  border-radius: 0 0 60px 0;
  margin-left: 17px;
}
.Lborder {
  position: absolute;
  top: -5px;
  left: -9px;
  width: 10px;
  // background-color: green;
  height: 10px;
  border-width: 0px 10px 10px 0;
  border-style: solid;
  border-bottom-color: inherit;
  /*自动继承父元素的border-bottom-color*/
  border-right-color: transparent;
  border-radius: 0 0px 0px 60px;
}
@media screen and (max-width: 765px) {
  .tip {
    color: #fff;
  }
}
</style>
